<template>
  <el-drawer
    v-model="props.drawerVisibility"
    :title="title"
    :size="size"
    :before-close="handleClose"
  >
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="10rem">
      <el-form-item label="角色识别：">
        <el-input disabled v-model="form.id" />
      </el-form-item>
      <el-form-item prop="name" label="角色名称：">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色备注：">
        <el-input v-model="form.remark" />
      </el-form-item>
      <el-form-item label="角色权限：" prop="roleKey">
        <el-select
          v-model="form.roleKey"
          class="m-2"
          :placeholder="form.roleKey"
          size="large"
          prop="roleKey"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :type="ElButtonType" @click="submit(ruleFormRef)"
          >提交</el-button
        >
        <el-button @click="handleClear">重置</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script lang="ts" setup>
import { themePinia } from "@/libs/pinia/theme";
import { onMounted, reactive, ref, toRefs, computed } from "vue";
import type { FormInstance, FormRules  } from "element-plus";
import  { ElNotification  } from "element-plus";

const props = defineProps({
  drawerVisibility: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: "详细信息",
  },
  size: {
    type: String,
    default: "30%",
  },
  form: {
    type: Object,
    default: {},
  },
  flag: {
    type: Boolean,
    default: false,
  },
});

const info: any = reactive({
  id: "",
  name: "",
  remark: "",
  roleKey: "",
});

onMounted(() => {
  // toRefs 将响应式对象，转换为可响应式引用
});

const ruleFormRef = ref<FormInstance>();

// 验证规则
const rules = reactive<FormRules<any>>({
  name: [
    { required: true, message: "角色名称是必填项！", trigger: "blur" },
    { min: 3, max: 8, message: "角色名称长度应该在3-8之间！", trigger: "blur" },
  ],
  roleKey: [{ required: true, message: "必须分配权限！", trigger: "blur" }],
});

const handleClear = () => {
  //重置
  info.name = "";
  info.remark = "";
  info.roleKey = "";
};

const emits = defineEmits(["handleCloseEmit", "handleSubmitEmit"]);

const handleClose = () => {
  // 判断是否有值进行关闭
  emits("handleCloseEmit");
};

const submit = async (infos: any) => {
  await infos.validate((valid: any, fields: any) => {
    if (valid) {
      info.id=props.form.id
      info.name=props.form.name
      info.remark=props.form.remark
      info.roleKey=props.form.roleKey
      emits("handleSubmitEmit", { ...info });
    } else {
      ElNotification({
        title: "错误",
        message: "This is an error message",
        type: "error"
      });
    }
  });
};

// 多选框
const options = [
  {
    value: "1",
    label: "房产管理员",
  },
  {
    value: "2",
    label: "图书管理员",
  },
];

const theme = themePinia();

const ElinputBackgroundColor = computed(() => theme.ElinputBackgroundColor);
const ElinputTextColor = computed(() => theme.ElinputTextColor);
const ElinputBorderColor = computed(() => theme.ElinputBorderColor);
const ElinputOpacity = computed(() => theme.ElinputOpacity);
const ElinputHoverColor = computed(() => theme.ElinputHoverColor);
const ElButtonType = computed(() => theme.ElButtonType as any);
</script>

<style scoped lang="scss">
el-row {
  margin-top: 1.2rem;
}
.image-box {
  display: flex;
  height: 100px;
  margin-top: 1.2rem;
}
:deep(.el-form-item) {
  display: flex;
  align-items: center;
}
:deep(.m-2) {
  margin: 0;
}
:deep(.el-form-item__label) {
  font-size: 1.2rem;
  /* margin-top: 0.3rem; */
}
:deep(.el-input__wrapper) {
  //移除主盒子样式
  padding: 0;
  box-shadow: none !important;
  background-color: rgba($color: #000000, $alpha: 0) !important;
  flex-grow: 0;
  .el-input__inner {
    background-color: v-bind(ElinputBackgroundColor);
    color: v-bind(ElinputTextColor);
    border: 1px;
    border-color: #999;
    border-style: solid;
    font-size: 1rem;
    padding: 0.5rem 1rem;
    transition: all 250ms ease;
    border-radius: 0.4rem;
    &:hover {
      border-color: v-bind(ElinputHoverColor);
    }
  }
}
</style>
